<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Asset Manager
 *
 * Software to automatically manage how assets are delivered on a website
 *
 * @package		Asset Manager
 * @author		Adam Lewis <adam.lewis@bestservedcold.com>
 * @copyright	Copyright (c) 2006 - 2012, Best Served Cold Media Limited.
 * @license		http://bestservedcold.com/license.html
 * @link		http://bestservedcold.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Single Query Assets Library
 *
 * The Asset library simply checks the asset database and returns the asset location.  If the asset
 * is not in the database, then it calls the asset library and queues the asset for processing.
 *
 * @package		Asset Manager
 * @subpackage	Assets
 * @category	Libraries
 * @author  	Adam Lewis <adam.lewis@bestservedcold.com>
 * @since		2012-05-22
 */
require_once(APPPATH.'libraries/Asset_manager.php');
class Am_multiple_query extends Asset_manager {

	/**
	 * Constructor
	 *
	 * @access		public
	 * @since		2012-05-22
	 * @return		void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Do work
	 *
	 * Does all the work on the fly as the page is being constrcuted.  Different to the single query method which is
	 * much more efficient.
	 *
	 * @access	protected
	 * @since	2012-05-22
	 * @param	mixed		$param
	 * @param	integer		$group
	 * @return	mixed
	 */
	public function get_asset_src($param, $group = FALSE)
	{
		// Get the asset by either the integer id or the dual file and group identifier
		$asset = is_int($param) ? $this->get_asset_by_id($param) : $this->get_asset($param, $group);

		// If that's in the database, then we're good to go
		if ($asset->num_rows)
		{
			// Get the asset row and let's start working with it
			$asset = $asset->row();

			// Check the asset, see if it needs some attention
			$asset = $this->check_asset($asset);

			// Return the asset src
			return $this->single_asset_src($asset);

		}
		// Okay, we're still here, is $param an integer?  Then we don't have that in the database
		if (is_int($param))
		{
			log_message('error', "[ASSETS] id: [$id] doesn't exist in the database");
			return FALSE;
		}

		// Well that asset is new then, let's process it
		return $this->process_asset($param, $group);
	}

	/**
	 * Get Asset
	 *
	 * Returns the asset from the database
	 *
	 * @access	private
	 * @since	2012-05-30
	 * @param	string		$file
	 * @param	integer		$group
	 * @return	mixed
	 */
	private function get_asset($param, $group = FALSE)
	{
		$params['file'] = $this->param;
		$group ? $params['group'] = $group : NULL;
		return $this->CI->db->select('cdn, base64, filetype')
			->from($this->CI->config->item('asset_table', 'asset_manager'))
			->where($params)->get();

	}

	/**
	 * Get Asset By Id
	 *
	 * If you want to use the unique identifier for speed and you've made your system clever enough
	 * just get the asset location by id.  Bit rough and dirty, if the asset's not there, it'll
	 * just return false.
	 *
	 * @access		public
	 * @since		2012-05-25
	 * @param 		integer 	$id
	 * @return		mixed
	 */
	private function get_asset_by_id($param)
	{
		return $this->CI->db->select('cdn, base64, filetype')
			->from($this->CI->config->item('asset_table', 'asset_manager'))->where(array('id' => $param))
			->get();
	}
}